﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Microsoft.Reporting.WebForms;
using NetDon.OA.Interface;
using NetDon.OA.Interface.eLogistics;
using NetDon.Utilities;

namespace NetDon.OA.Portal.eLogisticsQuery
{
    public partial class ReportView : PageBase
    {
        public string ReportCode
        {
            get { return Parameters.GetString("ReportCode"); }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            this.ReportViewer1.LocalReport.SubreportProcessing += new SubreportProcessingEventHandler(LocalReport_SubreportProcessing);
            if (!this.IsPostBack)
            {
                // Set the processing mode for the ReportViewer to Local
                this.ReportViewer1.ProcessingMode = ProcessingMode.Local;
                this.ReportViewer1.LocalReport.DataSources.Clear();

                GetReportSource();
                SetReportParameter();

                //this.ReportViewer1.LocalReport.Refresh();
            }

        }

        void LocalReport_SubreportProcessing(object sender, SubreportProcessingEventArgs e)
        {
            if (ReportCode == "AccountReceive")
            {
                var critiera = Session["AccountReceiveInvoceReceipts"] as RevenueMachineAccountInvoiceReceiptsSearchCriteria;
              
                var service = ServiceContainer.GetService<IRevenueMachineAccountService>();
                var dataTable = service.GetRevenueMachineAccountInvoiceReceipts(e.Parameters["Id"].Values[0].ToGuid(),critiera);

                
                e.DataSources.Add(new ReportDataSource("RevenueMachineAccountInvoiceReceiptDS", dataTable));
            }
            else if (ReportCode=="PaymentMachineAccountQuery")
            {
                var critiera = Session["PaymentMachineAccountQuery"] as MachineAccountSearchReportCriteria;

                var service = ServiceContainer.GetService<IReportQueryService>();
                critiera.IssuingOrderId = e.Parameters["Id"].Values[0].ToGuid();
                var dataTable = service.QueryPaymentMachineAccountDetails(critiera, "QueryPaymentMachineAccountDS");


                e.DataSources.Add(new ReportDataSource("QueryPaymentMachineAccountDS", dataTable));
            }

     
        }
        void SetReportParameter()
        {
            LocalReport localReport = ReportViewer1.LocalReport;
            // Create the sales order number report parameter
            ReportParameter rpUserName = new ReportParameter();
            rpUserName.Name = "UserName";
            rpUserName.Values.Add(CurrentApplicationContext.UserName);

            //// Set the report parameters for the report
            localReport.SetParameters(
                new ReportParameter[] { rpUserName });

            //if (ReportCode == "ReceiptInfoQuery")
            //{
            //    var critiera = Session[ReportCode] as ReceiptInfoQueryCriteria;
            //    var dateStart = new ReportParameter();
            //    dateStart.Name = "ReceiptInfoPlannedResponseDateStart";
            //    dateStart.Values.Add(critiera.ReceiptInfoPlannedResponseDate !=null &&critiera.ReceiptInfoPlannedResponseDate.Start.HasValue ? critiera.ReceiptInfoPlannedResponseDate.Start.Value.ToString("yyyy-MM-dd") : string.Empty);

            //    //// Set the report parameters for the report
            //    localReport.SetParameters(
            //        new ReportParameter[] { dateStart });


            //    var dateEnd = new ReportParameter();
            //    dateEnd.Name = "ReceiptInfoPlannedResponseDateEnd";
            //    dateEnd.Values.Add(critiera.ReceiptInfoPlannedResponseDate != null && critiera.ReceiptInfoPlannedResponseDate.End.HasValue ? critiera.ReceiptInfoPlannedResponseDate.End.Value.ToString("yyyy-MM-dd") : string.Empty);
            //    //// Set the report parameters for the report
            //    localReport.SetParameters(
            //        new ReportParameter[] { dateEnd });

            //     dateStart = new ReportParameter();
            //     dateStart.Name = "ActualShippingDateStart";
            //     dateStart.Values.Add(critiera.ActualShippingDate != null && critiera.ActualShippingDate.Start.HasValue ? critiera.ActualShippingDate.Start.Value.ToString("yyyy-MM-dd") : string.Empty);

            //    //// Set the report parameters for the report
            //    localReport.SetParameters(
            //        new ReportParameter[] { dateStart });


            //     dateEnd = new ReportParameter();
            //     dateEnd.Name = "ActualShippingDateEnd";
            //    dateEnd.Values.Add(critiera.ActualShippingDate != null && critiera.ActualShippingDate.End.HasValue ? critiera.ActualShippingDate.End.Value.ToString("yyyy-MM-dd") : string.Empty);
            //    //// Set the report parameters for the report
            //    localReport.SetParameters(
            //        new ReportParameter[] { dateEnd });

            //}

        }
        void GetReportSource()
        {
            LocalReport localReport = ReportViewer1.LocalReport;

            if (ReportCode == "QueryIssuingOrderPage")
            {
                var critiera = Session[ReportCode] as IssuingOrderSearchCriteria;
                localReport.ReportPath = "eLogisticsQuery/Reports/QueryIssuingOrder.rdlc";
                var service = ServiceContainer.GetService<IReportQueryService>();
                var dataTable = service.QueryIsuingOrderQuery(critiera, "IssuingOrderDS");

                ReportDataSource dsIssuingOrder = new ReportDataSource();
                dsIssuingOrder.Name = "IssuingOrderDS";
                dsIssuingOrder.Value = dataTable;

                localReport.DataSources.Add(dsIssuingOrder);

                dataTable = service.QueryIsuingOrderQueryDelivery(critiera, "IssuingOrderDeliveryDS");
                var dsIssuingOrderDelivery = new ReportDataSource();
                dsIssuingOrderDelivery.Name = "IssuingOrderDeliveryDS";
                dsIssuingOrderDelivery.Value = dataTable;

                localReport.DataSources.Add(dsIssuingOrderDelivery);

            }
            else if (ReportCode == "OnsiteEntruckingQueryPage")
            {
                var critiera = Session[ReportCode] as IssuingOrderSearchCriteria;
                localReport.ReportPath = "eLogisticsQuery/Reports/QueryOnsiteEntrucking.rdlc";
                var service = ServiceContainer.GetService<IReportQueryService>();
                var dataTable = service.QueryOnsiteEntruckingQuery(critiera, "OnsiteEntruckingDS");

                ReportDataSource dsIssuingOrder = new ReportDataSource();
                dsIssuingOrder.Name = "OnsiteEntruckingDS";
                dsIssuingOrder.Value = dataTable;

                localReport.DataSources.Add(dsIssuingOrder);

            }
            else if (ReportCode == "OnWayTrackQueryPage")
            {
                var critiera = Session[ReportCode] as IssuingOrderOnWayTrackSearchCriteria;

                localReport.ReportPath = "eLogisticsQuery/Reports/QueryOnwayTrack.rdlc";
                var service = ServiceContainer.GetService<IReportQueryService>();
                var dataTable = service.QueryOnwayTrackQuery(critiera, "QueryOnwayTrackDS");
                ReportDataSource dsIssuingOrder = new ReportDataSource();
                dsIssuingOrder.Name = "QueryOnwayTrackDS";
                dsIssuingOrder.Value = dataTable;

                localReport.DataSources.Add(dsIssuingOrder);

            }
            else if (ReportCode == "PaymentMachineAccountQuery")
            {
                var critiera = Session[ReportCode] as MachineAccountSearchReportCriteria;


                localReport.ReportPath = "eLogisticsQuery/Reports/QueryPaymentMachineAccount.rdlc";
                var service = ServiceContainer.GetService<IReportQueryService>();
                var dataTable = service.QueryPaymentMachineAccount(critiera, "QueryPaymentMachineAccountDS");

                ReportDataSource dsIssuingOrder = new ReportDataSource();
                dsIssuingOrder.Name = "QueryPaymentMachineAccountDS";
                dsIssuingOrder.Value = dataTable;

                localReport.DataSources.Add(dsIssuingOrder);

            }
            else if (ReportCode == "ReceiptInfoTrackQueryPage")
            {
                var critiera = Session[ReportCode] as IssuingOrderReceiptInfoTrackSearchCriteria;

                localReport.ReportPath = "eLogisticsQuery/Reports/QueryReceiptInfoTrack.rdlc";
                var service = ServiceContainer.GetService<IReportQueryService>();
                var dataTable = service.QueryReceiptInfoTrackQuery(critiera, "QueryReceiptInfoTrackDS");


                ReportDataSource dsIssuingOrder = new ReportDataSource();
                dsIssuingOrder.Name = "QueryReceiptInfoTrackDS";
                dsIssuingOrder.Value = dataTable;

                localReport.DataSources.Add(dsIssuingOrder);


            }
            else if (ReportCode == "ShuntingTrackInfoQueryPage")
            {
                var critiera = Session[ReportCode] as IssuingOrderSearchCriteria;

                localReport.ReportPath = "eLogisticsQuery/Reports/QueryShuntingTrackInfo.rdlc";
                var service = ServiceContainer.GetService<IReportQueryService>();
                var dataTable = service.QueryShuntingTrackInfoQuery(critiera, "ShuntingTrackInfoDS");

                ReportDataSource dsIssuingOrder = new ReportDataSource();
                dsIssuingOrder.Name = "ShuntingTrackInfoDS";
                dsIssuingOrder.Value = dataTable;

                localReport.DataSources.Add(dsIssuingOrder);

            }
            else if (ReportCode == "RevenueMachineAccountQuery")
            {
                var critiera = Session[ReportCode] as RevenueMachineAccountSearchCriteria;

                localReport.ReportPath = "eLogisticsQuery/Reports/RevenueMachineAccount.rdlc";
              
                var service = ServiceContainer.GetService<IRevenueMachineAccountService>();
                var dataTable = service.SearchRevenueMachineAccountDataByConditionForReport(critiera, "", SortDirection.Ascending, 9999999, 0);

                ReportDataSource dsIssuingOrder = new ReportDataSource();
                dsIssuingOrder.Name = "RevenueMachineAccountDS";
                dsIssuingOrder.Value = dataTable;

                localReport.DataSources.Add(dsIssuingOrder);
            }
            else if (ReportCode == "AccountReceive")
            {
                var critiera = Session["AccountReceiveInvoice"] as RevenueMachineAccountInvoiceSearchCriteria;
                localReport.ReportPath = "eLogisticsQuery/Reports/AccountReceiveQuery.rdlc";
                var service = ServiceContainer.GetService<IRevenueMachineAccountService>();
                var dataTable = service.SearchRevenueMachineAccountInvoiceByCondition(critiera, "", SortDirection.Ascending, 9999999, 0);
                ReportDataSource dsIssuingOrder = new ReportDataSource();
                dsIssuingOrder.Name = "RevenueMachineAccountDS";
                dsIssuingOrder.Value = dataTable;

                localReport.DataSources.Add(dsIssuingOrder);


            }
            else if (ReportCode == "ProfitQuery")
            {
                var critiera = Session[ReportCode] as RevenueMachineAccountSearchCriteria;
                localReport.ReportPath = "eLogisticsQuery/Reports/ProfitQuery.rdlc";

                var service = ServiceContainer.GetService<IRevenueMachineAccountService>();
                var dataTable = service.SearchRevenueMachineAccountProfitByConditionForReport(critiera, "", SortDirection.Ascending, 9999999, 0);
                ReportDataSource dsIssuingOrder = new ReportDataSource();
                dsIssuingOrder.Name = "RevenueMachineAccountDS";
                dsIssuingOrder.Value = dataTable;

                localReport.DataSources.Add(dsIssuingOrder);
            }
            else if (ReportCode == "InvoiceQuery")
            {

                var critiera = Session[ReportCode] as RevenueMachineAccountInvoiceSearchCriteria;
                localReport.ReportPath = "eLogisticsQuery/Reports/InvoiceQuery.rdlc";

                var service = ServiceContainer.GetService<IRevenueMachineAccountService>();
                var dataTable = service.SearchRevenueMachineAccountInvoiceByConditionForReport(critiera, "", SortDirection.Ascending, 9999999, 0);
                ReportDataSource dsIssuingOrder = new ReportDataSource();
                dsIssuingOrder.Name = "RevenueMachineAccountDS";
                dsIssuingOrder.Value = dataTable;

                localReport.DataSources.Add(dsIssuingOrder);

            }
            else if (ReportCode == "DuePaymentMachineQuery")//扣罚查询
            {
                var critiera = Session[ReportCode] as MachineAccountSearchCriteria;
                localReport.ReportPath = "eLogisticsQuery/Reports/DuePaymentMachineQuery.rdlc";
                var service = ServiceContainer.GetService<IReportQueryService>();
                var dataTable = service.QueryPaymentMachineAccountDeduction(critiera, "QueryPaymentMachineAccountDS");

                ReportDataSource dsIssuingOrder = new ReportDataSource();
                dsIssuingOrder.Name = "QueryPaymentMachineAccountDS";
                dsIssuingOrder.Value = dataTable;

                localReport.DataSources.Add(dsIssuingOrder);

            }

            else if (ReportCode == "ReceiptInfoQuery")
            {
                var critiera = Session[ReportCode] as ReceiptInfoQueryCriteria;
                localReport.ReportPath = "eLogisticsQuery/Reports/ReceiptInfoQuery.rdlc";

                var service = ServiceContainer.GetService<IReportQueryService>();
                var dataTable = service.QueryReceiptInfoQuery(critiera, "ReceiptInfoQueryDS");

                ReportDataSource dsIssuingOrder = new ReportDataSource();
                dsIssuingOrder.Name = "ReceiptInfoQueryDS";
                dsIssuingOrder.Value = dataTable;

   
                localReport.DataSources.Add(dsIssuingOrder);
            }
            else if (ReportCode == "GoodsShippingStatusQuery")
            {
                var critiera = Session[ReportCode] as GoodsShippingStatusQueryCriteria;
                localReport.ReportPath = "eLogisticsQuery/Reports/GoodsShippingOnWayTrackSummary.rdlc";


                var service = ServiceContainer.GetService<IReportQueryService>();
                var dataTable = service.QueryGoodsShippingOnWayTrackSummary(critiera, "GoodsShippingOnWayTrackSummaryDS");

                ReportDataSource dsIssuingOrder = new ReportDataSource();
                dsIssuingOrder.Name = "GoodsShippingOnWayTrackSummaryDS";
                dsIssuingOrder.Value = dataTable;

                localReport.DataSources.Add(dsIssuingOrder);
            }


        }
    }
}